<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
<!--      <div class="avater_box">
        <img src="../assets/logo.png" alt="" />
      </div> -->
      <div class="title">
        <h3>NDS 播出后台管理系统</h3>
      </div>
      <!-- 登录表单区域 -->
      <el-form size="small" ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            @keyup.enter.native="login"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            type="password"
            @keyup.enter.native="login"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="loginForm.rememberMe">记住我</el-checkbox>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click.enter="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <p class="company">安徽励图信息科技股份有限公司 © 版权所有 服务热线：0551-65322628 support@ltech.cc www.ltech.com.cn</p>
  </div>
</template>

<script>
import { encrypt, decrypt } from '@/utils/rsaEncrypt'
import Config from '@/settings'
import Cookies from 'js-cookie'
export default {
  data () {
    return {
      loginForm: {
        username: '',
        password: '',
        rememberMe: false
      },
      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户是否合法
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 获取用户名密码等Cookie
    this.getCookie()
    console.log(encrypt('abc'))
    console.log(decrypt('VsMMsLBNIAi6PlznGyjTifSgGP8Z9Hx3gZCPQaXanVa5BDSmGBte3zWGsajPnxLLxgvMsnSaghPKLwuAo2EDKg=='))
  },
  methods: {
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password') ? decrypt(Cookies.get('password')) : this.loginForm.password
      const rememberMe = Boolean(Cookies.get('rememberMe'))

      this.loginForm = {
        username,
        password,
        rememberMe
      }
      // console.log(username, password, rememberMe)
    },
    // 点击重置按钮，重置登录表单
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
      console.log(this)
    },
    login () {
      // 表单预验证
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return // 验证失败

        const user = {
          username: this.loginForm.username,
          password: this.loginForm.password,
          rememberMe: this.loginForm.rememberMe
        }

        if (user.rememberMe) {
          Cookies.set('username', user.username, { expires: Config.passCookieExpires })
          Cookies.set('password', encrypt(user.password), { expires: Config.passCookieExpires })
          Cookies.set('rememberMe', user.rememberMe, { expires: Config.passCookieExpires })
        } else {
          Cookies.remove('username')
          Cookies.remove('password')
          Cookies.remove('rememberMe')
        }

        this.$store.dispatch('Login', user).then(() => {
          this.$router.push({ name: '/' })
          return this.$message.success('登录成功')
        }).catch((err) => {
          console.log(err)
        })

        // const { data: res } = await this.$http.post('login', this.loginForm)

        // if (res.meta.status !== 200) return this.$message.error(res.meta.message)

        // 1. 将登录成功之后的taken，保存到客户端的 sessionStorage 中
        // 1.1 项目中除了登录之外的其他API接口， 必须在登陆之后才能访问
        // 1.2 token 只应在当前网站打开期间生效，所以将token 保存在sessionStorage 中
        // 2.通过编程式导航跳转到后台主页，路由地址是 /home
        // this.$localStorage.set('token', res.data.token)
        // this.$localStorage.set('userid', res.data.userid)
        // this.$router.push({ name: '/' })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
    background : url(../assets/loginbg.jpg) no-repeat center center;
    background-size: cover;
    height: 100%;
    //display: flex;
    //justify-content: center;
    //align-items: center;
    .company {
      font-size: 12px;
      width: 100%;
      text-align: center;
      color: #fff;
      position: absolute;
      bottom: 10px;
      margin: 0 auto;
    }
}
.login_box{
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  .avater_box{
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #fff;
    img{
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .title {
    margin-top: 30px;
    h3{
      text-align: center;
      color: #707070;
    }
  }
  .login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .btns{
    display: flex;
    justify-content: flex-end;
  }
}
</style>
